package com.fjnldx.ks.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fjnldx.ks.mapper.CommonMapper;
import com.fjnldx.ks.mapper.UserMapper;
import com.fjnldx.ks.pojo.mesDoMain.User;
import com.fjnldx.ks.pojo.response.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CommonService {
    @Resource
    CommonMapper commonMapper;
    @Resource
    UserMapper userMapper;
    public String getPassword(String username) {
        return commonMapper.getPassword(username);
    }
    public String getRole(String username) {
        return commonMapper.getRole(username);
    }
    public String getUserUuid(String username) {
        return commonMapper.getUserUuid(username);
    }

    public void updateUserUuid(String username, String uuid) {
        commonMapper.updateUserUuid(username, uuid);
    }
    /**
     * 用户注册
     *
     * @param user
     * @return
     */
    public Result userRegistration(User user) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", user.getUsername());
        List<User> users = userMapper.selectList(queryWrapper);
        if (users.size() > 0) {
            return Result.error("已存在该用户");
        }
        user.setRole("common");
        int i = userMapper.insert(user);
        return i > 0 ? Result.success("注册成功") : Result.error("系统异常");
    }

    /**
     * 修改密码
     * @param user
     * @return
     */
    public Result changePassword(User user) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", user.getUsername());
        List<User> users = userMapper.selectList(queryWrapper);
        if (users.size() == 0) {
            return Result.error("不存在该用户，请先注册");
        }
        int i = userMapper.update(user, queryWrapper);
        return i > 0 ? Result.success("更新成功") : Result.error("系统异常");
    }
}
